import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';

class SelectMotorcadeController extends GetxController {
  MultiState multiState = MultiState.loading;
  List<MotorcadeEntity> motorcadeList = [];

  @override
  void onReady() {
    super.onReady();
    getMotorcadeList();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getMotorcadeList();
  }

  Future<void> getMotorcadeList() async {
    httpManager.post(
      Get.context,
      HttpAddress.GET_MOTORCADE_LIST,
      {"superId": await SPUtils.getString(SPUtils.COMPANY_ID)},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          multiState = MultiState.fail;
          update();
          return;
        }
        if (data is List) {
          motorcadeList = data.map((element) {
            return MotorcadeEntity().fromJson((element as Map<String, dynamic>));
          }).toList();
          multiState = motorcadeList.isNotEmpty ? MultiState.success : MultiState.empty;
          update();
        }
      },
      onFail: (code, msg) {
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        multiState = MultiState.error;
        update();
      },
    );
  }
}
